package com.taxi.driver.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mob.MobSDK;
import com.mob.tools.utils.BitmapHelper;
import com.qianxx.utils.ToastUtil;
import com.taxi.driver.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


public class ShareDialog implements View.OnClickListener, PlatformActionListener {

    private Dialog dialog;
    private Display display;
    private Context mContext;

    private String title;
    private String text;
    private String url;

    private TextView tvTitle;

    public ShareDialog(Context context) {
        this.mContext = context;
        WindowManager windowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public void show() {
        dialog.show();
    }

    public ShareDialog builder() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_share, null);

        view.setMinimumWidth(display.getWidth());

        dialog = new Dialog(mContext, R.style.alert_dialog);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        dialog.setCanceledOnTouchOutside(true);

        tvTitle = (TextView) dialog.findViewById(R.id.tv_title);

        LinearLayout llWechat = (LinearLayout) dialog.findViewById(R.id.ll_wechat);
        llWechat.setOnClickListener(this);
        LinearLayout llCircle = (LinearLayout) dialog.findViewById(R.id.ll_circle);
        llCircle.setOnClickListener(this);
        LinearLayout llQq = (LinearLayout) dialog.findViewById(R.id.ll_qq);
        llQq.setOnClickListener(this);
        LinearLayout llSpace = (LinearLayout) dialog.findViewById(R.id.ll_space);
        llSpace.setOnClickListener(this);
        TextView tvCancel = (TextView) dialog.findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
        LinearLayout llFaceBook = (LinearLayout) dialog.findViewById(R.id.ll_facebook);
        llFaceBook.setOnClickListener(this);

        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_wechat: {
                initPlatform(Wechat.NAME);
                dialog.dismiss();
                break;
            }
            case R.id.ll_circle: {
                initPlatform(WechatMoments.NAME);
                dialog.dismiss();
                break;
            }
            case R.id.ll_qq: {
                initPlatform(QQ.NAME);
                dialog.dismiss();
                break;
            }
            case R.id.ll_space: {
                initPlatform(QZone.NAME);
                dialog.dismiss();
                break;
            }
            case R.id.ll_facebook:
//                initPlatform(Facebook.NAME);
                dialog.dismiss();
                break;
            case R.id.tv_cancel:
                dialog.dismiss();
                break;
        }
    }

    public ShareDialog setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        return this;
    }

    public ShareDialog setContent(String title, String text, String url) {
        this.title = title;
        this.text = text;
        this.url = url;
        return this;
    }

    private void initPlatform(String platform) {
        MobSDK.init(mContext);

        Platform.ShareParams sp = new Platform.ShareParams();
        if (!platform.equals(QZone.NAME)) {
            sp.setTitle(title);
        }
        sp.setText(text);
        if (platform.equals(Wechat.NAME) || platform.equals(WechatMoments.NAME)
                ) {
            sp.setUrl(url);
            sp.setShareType(Platform.SHARE_WEBPAGE);
        }
        if (platform.equals(QQ.NAME) || platform.equals(QZone.NAME)) {
            sp.setTitleUrl(url);
        }
        if (platform.equals(QZone.NAME)) {
            sp.setSite(mContext.getResources().getString(R.string.app_name));
        }
        sp.setImagePath(changeResource2File(mContext, R.drawable.share_icon));
        Platform plat = ShareSDK.getPlatform(platform);
        plat.setPlatformActionListener(this);
        plat.share(sp);
    }

    private static String changeResource2File(Context context, @DrawableRes int res) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
        try {
            return BitmapHelper.saveBitmap(context, bitmap);
        } catch (Throwable throwable) {
            return null;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String content = (String) msg.obj;
            ToastUtil.getInstance().toast(content);
        }
    };

    @Override
    public void onComplete(cn.sharesdk.framework.Platform platform, int i, HashMap<String, Object> hashMap) {
        ToastUtil.getInstance().toast("分享成功");
    }

    @Override
    public void onError(cn.sharesdk.framework.Platform platform, int i, Throwable throwable) {
        // 操作失败的处理代码
        String expName = throwable.getClass().getSimpleName();
        Message msg = new Message();
        //判断有没有安装客户端
        if ("WechatClientNotExistException".equals(expName)
                || "WechatTimelineNotSupportedException".equals(expName)
                || "WechatFavoriteNotSupportedException".equals(expName)) {
            msg.obj = "没有安装微信";
        } else if ("QQClientNotExistException".equals(expName)) {
            msg.obj = "没有安装QQ";
        }
        handler.sendMessage(msg);
    }

    @Override
    public void onCancel(cn.sharesdk.framework.Platform platform, int i) {
        ToastUtil.getInstance().toast("分享取消");
    }
}